import React from 'react';
import { inject, observer} from 'mobx-react';
import {WeaTableNew} from 'comsMobx';
import { Button } from 'antd';
import {WeaTab,WeaRightMenu,WeaTools,WeaErrorPage,WeaLocaleProvider,WeaDialog} from 'ecCom';
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

@observer
class ImportModelDialog extends React.Component {
    constructor(props) {
		super(props);
    }
    componentDidMount() {
    }
    componentWillReceiveProps(nextProps) {}
    render() {
        const { workflowNewDesignStore ,isFree=''} = this.props;
        const { saveAsTemplate , saveAsTempModalShow} = workflowNewDesignStore;
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@wyf7tt`}
                visible={saveAsTempModalShow}
                title={getLabel(18418,'存为模板')}
                icon="icon-coms-workflow"
                iconBgcolor='#0079DE'
                maskClosable={false}
                buttons={this.getModalBtns()}
                onCancel={() => saveAsTemplate(false)}
                style={{width: "500px",height:'150px'}}
                moreBtn={{datas:this.getRightMenu()}}
                layout = {window.parent.document.body}
                hasScroll
            ><WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@q5pih7`} 
                collectParams={{favname:getLabel(18418,'存为模板'), favouritetype:2}}
                datas={this.getRightMenu()}>
                    <div className="wf-free-flowset-saveAsTemplate" style={{paddingTop:'20px'}}>
                            {this.getContent()}
                    </div>
                </WeaRightMenu>
            </WeaDialog>
        )
    }
    getContent = () => {
        const { workflowNewDesignStore } = this.props;
        const { saveAsTempCondition , saveAsTempForm} = workflowNewDesignStore;
        const coms = saveAsTempForm.render({
            needTigger: false,
            showGroup:true,
            col:1
            }, {
                onChange:(v)=>{
                    for(let key in v){
                        if(key=='name'){
                            if(v[key].value.replace(/[^\u0000-\u00ff]/g,"aaa").length == 100) {
                                saveAsTempForm.showError('name', '"'+getLabel(18151, '模板名称')+'"' +getLabel(502997, '长度不能超过')+" 100 "+ getLabel(20075, '个字符'));
                            }
                        }
                    }
                }
        });
        return coms;
    }
    dataSave = () => {
        const { workflowNewDesignStore,reLoadTemplateList } = this.props;
        const { saveAsTempSave , saveAsTempForm} = workflowNewDesignStore;
         saveAsTempForm.validateForm().then(f=>{
            if(f.isValid){
                saveAsTempSave(reLoadTemplateList);
            }else{
                f.showErrors();
                this.setState({date: new Date()}); // 改变一个state的变量,强制页面刷新
                Modal.warning({
                    title: getLabel(15172,'系统提示'),
                    content: getLabel(383975,'必要信息不完整，红色标记为必填项！'),
                });
            }
        });
    }
    getModalBtns(){
        const { workflowNewDesignStore } = this.props;
        const { saveAsTempSave , saveAsTempForm} = workflowNewDesignStore;
        let btns = [];
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@aok6f1@save`} type="primary" onClick={()=>{
            this.dataSave();
        }}>{getLabel(86,'保存')}</Button>);
        return btns;
    }
    getRightMenu(){
        const { workflowNewDesignStore } = this.props;
        const { saveAsTempSave } = workflowNewDesignStore;
        let rtMenus = [];
        rtMenus.push({
            key: "save",
    		icon: <i className="icon-coms-Preservation"/>,
            content:getLabel(86,'保存'),
            onClick: key =>{this.dataSave();}
        });
    	return rtMenus;
    }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@uffqn8`} msg={ props.error ? props.error : getLabel(383324,' 对不起，该页面异常，请联系管理员 ')+'！'} />,
    {error: ""}
)(ImportModelDialog);